package com.itheima.health.service;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.health.common.PageParam;
import com.itheima.health.common.R;
import com.itheima.health.common.ResultPageData;
import com.itheima.health.mapper.CheckGroupDao;
import com.itheima.health.mapper.CheckGroupMapper;
import com.itheima.health.mapper.CheckItemDao;
import com.itheima.health.mapper.CheckItemMapper;
import com.itheima.health.model.dtos.AddCheckGroupDto;
import com.itheima.health.model.dtos.UpdateCheckGroupDto;
import com.itheima.health.model.pojos.CheckGroup;
import com.itheima.health.model.pojos.QueryString;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
public class CheckGroupService {
    @Autowired
    private CheckGroupDao checkGroupDao;
    @Autowired
    private CheckItemMapper checkItemMapper;
    @Autowired
    private CheckItemDao checkItemDao;
    @Autowired
    private CheckGroupMapper checkGroupMapper;

    public ResultPageData getPage(PageParam pageParam) {
        PageHelper.startPage(pageParam.getCurrentPage(),pageParam.getPageSize());
        QueryString queryString1 = new QueryString();
        String queryString=null;
        if (pageParam.getQueryString()!=null &&pageParam.getQueryString()!=""){
            queryString = pageParam.getQueryString();
            if (queryString.matches("[0-9]+")) {
                queryString1.setCode(queryString);
            } else if (queryString.matches("[a-zA-Z]+")) {
                queryString1.setHelpCode(queryString);
            }
            else {
                queryString1.setName(queryString);
            }
        }
        List<CheckGroup> checkGroupList= checkGroupMapper.getPage(queryString1);
        Page<CheckGroup> checkGroupList1 = (Page<CheckGroup>) checkGroupList;
        return new ResultPageData(checkGroupList1.getTotal(),checkGroupList1.getResult());
    }


    @Transactional(rollbackFor = Exception.class)
    public void addCheckGroup(AddCheckGroupDto dto,List<Integer> ids) {
        checkGroupMapper.insert(dto);

        checkGroupMapper.insertCheckItems(dto,ids);
    }

    public CheckGroup getCheckGroupById(Integer id) {
       return checkGroupMapper.selectById(id);
    }

    public void updateCheckGroupById(UpdateCheckGroupDto dto,List<Integer> ids) {
        checkGroupMapper.deleteCheckMsgByCheckGroupId(dto);

        checkGroupMapper.updateById(dto);
        if (!ids.isEmpty()){
            AddCheckGroupDto addCheckGroupDto = new AddCheckGroupDto();
            BeanUtils.copyProperties(dto,addCheckGroupDto);
            checkGroupMapper.insertCheckItems(addCheckGroupDto,ids);
        }

    }

    public R deleteCheckGroupById(Integer id) {
        R r = new R();
        List<Integer> checkItemsByCheckGroupId = checkItemMapper.getCheckItemsByCheckGroupId(id);
        if (checkItemsByCheckGroupId.isEmpty()){
            checkGroupMapper.deleteCheckGroupById(id);
            r.setFlag(true);
            r.setMessage("删除成功");
            return r;
        }
        else {
            r.setFlag(false);
            r.setMessage("删除失败");
            return r;
        }

    }

    public List<CheckGroup> getCheckGroupAll() {
       return checkGroupMapper.getCheckGroupAll();
    }

    public List<Integer> getCheckGroupIdsBySetMealId(Integer setMealId) {
      return   checkGroupMapper.getCheckGroupIdsBySetMealId(setMealId);
    }
}
